<?php

namespace App\Http\Controllers\Common;

use App\Console\Commands\OpenShop\RefreshProvinceDevelopers;
use App\Console\Commands\User\ExtUserUpdateV2;
use App\Http\Controllers\BaseController;
use App\Library\Rms\RmsUserApi;
use App\Models\Common\FsCompanyUserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class FsUserController extends BaseController
{
    public function getFsCompanyUsers(Request $request)
    {
        $appId   = (string)$request->get('appId');
        $userIds = (array)$request->get('userIds');

        $this->validate($request, [
            'appId'   => 'required|string',
            'userIds' => 'required|array',
        ]);

        $lists = FsCompanyUserModel::query()
                                   ->whereIn('rmsUserId', $userIds)
                                   ->where('appId', $appId)
                                   ->select([
                                       'rmsUserId',
                                       'appId',
                                       'feishuId',
                                       'name',
                                       'phone',
                                       'openId',
                                       'unionId',
                                       'avatar'
                                   ])
                                   ->get();

        return $this->success(['lists' => $lists]);
    }

    public function refreshFsExtUser(Request $request)
    {
        $departmentName = $request->get('departmentName');
        $orgId          = $request->get('orgId',0);

        $this->validate($request, [
            'orgId'          => 'required|integer',
//            'departmentName' => 'required|string',
        ]);

        (new ExtUserUpdateV2())->doHandle($orgId, $departmentName);
    }

    public function refreshProvinceDeveloper(Request $request)
    {
        $mobile = (string)$request->get('mobile');

        $this->validate($request, [
            'mobile' => 'required|string',
        ]);

        $userIds = [];
        if ($mobile) {
            $userIds = collect((new RmsUserApi())->getUserInfoByMobiles([$mobile])['data'] ?? [])
            ->pluck('id')
            ->toArray();
        }

        // 刷新省区开发
        Artisan::call('openShop:refresh-province-developers');

        //(new RefreshProvinceDevelopers())->doHandle($userIds);
    }
}
